Debian Codegraph @

gtk-priv/scripts/repo.py

raw
1 '''
2 MIT License
3
4 Copyright 2020 Sophie Winter
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 '''
12
13 import subprocess
14 import logging
15 from os import path
16
17 logger = logging.getLogger(__name__)
18
19 class Repo:
20 def __init__(self, repo_url, repo_dir):
21 self.repo_dir = repo_dir
22 if not path.exists(self.repo_dir):
23 logger.info('Cloning ' + repo_url + ' into ' + self.repo_dir)
24 subprocess.run(
25 ['git', 'clone', repo_url, self.repo_dir]
26 ).check_returncode()
27 else:
28 logger.info('Fetching')
29 subprocess.run(
30 ['git', 'fetch'],
31 cwd=self.repo_dir
32 ).check_returncode()
33
34 def get_branches(self):
35 result = subprocess.run(
36 ['git', 'branch', '-r'],
37 capture_output=True,
38 encoding='utf-8',
39 cwd=self.repo_dir)
40 result.check_returncode()
41 branches = result.stdout.splitlines()
42 # strip of origin/
43 branches = [branch.split('/')[-1] for branch in branches]
44 logger.info('Found ' + str(len(branches)) + ' git branches')
45 return branches
46
47 def get_tags(self):
48 '''Returns a list of git tags'''
49 result = subprocess.run(
50 ['git', 'tag', '-l'],
51 capture_output=True,
52 encoding='utf-8',
53 cwd=self.repo_dir)
54 result.check_returncode()
55 tags = result.stdout.splitlines()
56 logger.info('Found ' + str(len(tags)) + ' git tags')
57 return tags
58
59 def checkout(self, tag):
60 '''Checks out a branch or tag'''
61 # logger.info('Checking out ' + tag)
62 result = subprocess.run(
63 ['git', 'checkout', tag],
64 cwd=self.repo_dir,
65 capture_output=True
66 ).check_returncode()