forked from paparazzi/paparazzi
-
Notifications
You must be signed in to change notification settings - Fork 2
/
paparazzi_pkgman.py
executable file
·193 lines (159 loc) · 5.93 KB
/
paparazzi_pkgman.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python
#
# Copyright (C) 2012-2014 The Paparazzi Team
#
# This file is part of Paparazzi.
#
# Paparazzi is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# Paparazzi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with paparazzi; see the file COPYING. If not, see
# <http://www.gnu.org/licenses/>.
#
from __future__ import print_function
import argparse
import os
import sys
import subprocess
import distutils.dir_util
import git #sudo pip install gitpython
PACKAGES_FILE = "./.packages"
CONSOLE_NORMAL = "\033[0m"
CONSOLE_BOLD = "\033[1m"
CONSOLE_SHA = "\033[0;33m"
class Package(object):
def __init__(self, nr, name):
self._temp_dir_ = "./var/pkgman/"
self.nr = nr
self.name = name
self.url = ""
self.lpath = ""
self.lcommit = ""
self.rpath = ""
self.rcommit = ""
def print(self,verbose):
print(CONSOLE_BOLD + str(self.nr), ": ", self.name, CONSOLE_NORMAL)
print(" -", self.url)
print(" -", self.rpath, "->", self.lpath)
if verbose:
print(" -" + CONSOLE_SHA, self.rcommit, CONSOLE_NORMAL + "->" + CONSOLE_SHA, self.lcommit,CONSOLE_NORMAL)
print(" -", self.git_temp())
# local git working directory
def git_temp(self):
return self._temp_dir_ + self.name.replace(" ","_").replace("/","_")
packages = []
def copy(src, dst):
print(" - copy",src, dst)
try:
distutils.dir_util.copy_tree(src, dst)
except:
raise
def read():
p = None
p_id = 1
with open(PACKAGES_FILE) as f:
for line in f:
if "[package" in line:
p = Package(p_id, line.replace("[package","").replace("]","").replace("\"","").strip())
packages.append(p)
p_id = p_id + 1
elif p is not None:
if "lpath" in line:
p.lpath = line.replace("lpath","").replace("=","").strip()
if "rpath" in line:
p.rpath = line.replace("rpath","").replace("=","").strip()
if "url" in line:
p.url = line.replace("url","").replace("=","").strip()
if "lcommit" in line:
p.lcommit = line.replace("lcommit","").replace("=","").strip()
if "rcommit" in line:
p.rcommit = line.replace("rcommit","").replace("=","").strip()
def store_commit(old_sha, new_sha):
with open(PACKAGES_FILE, "r+") as f:
old = f.read()
f.seek(0)
f.write(old.replace(old_sha, new_sha))
read()
def pkgman_clean(args):
print("Clean:\n-----\n")
cmd = ["rm", "-rf", Package(0,'')._temp_dir_]
if args.verbose:
print(cmd)
out = subprocess.call(cmd)
print(out)
def verify(p,args):
print(CONSOLE_BOLD + "Package:", p.name,CONSOLE_NORMAL)
if not os.path.exists(p.git_temp()):
print(" - creating", p.git_temp())
os.makedirs(p.git_temp())
print(" - git clone", p.url)
git.Repo.clone_from(p.url, p.git_temp())
def pkgman_list(args):
print("List:\n----\n")
for p in packages:
p.print(args.verbose)
def pkgman_status(args):
for p in packages:
verify(p,args)
g1 = git.Repo(p.git_temp()).git
txt = g1.rev_list( p.rcommit+"..HEAD", "--count")
print(" - remote commits since last merge:\t",txt)
if args.verbose:
print(' - git log:')
print(g1.log(p.rcommit+"..HEAD", '--pretty=format: * '+CONSOLE_SHA+'%h'+CONSOLE_NORMAL+' %s, %cr, %aN', '--abbrev-commit'))
g2 = git.Repo("./").git
txt = g2.rev_list(p.lcommit+"..HEAD", "--count")
print(" - local commits since last merge:\t",txt)
if args.verbose:
print(' - git log:')
print(g2.log(p.lcommit+"..HEAD", '--pretty=format: * '+CONSOLE_SHA+'%h'+CONSOLE_NORMAL+' %s, %cr, %aN', '--abbrev-commit'))
def pkgman_update(args):
print("Update:\n------\n")
for p in packages:
verify(p,args)
print(" - git update",p.git_temp())
g = git.Repo(p.git_temp()).git
txt = g.pull()
if (args.verbose):
print(txt)
# copy remote to local
copy( p.git_temp() + "/" + p.rpath, p.lpath)
# store new sha
sha = g.rev_parse("HEAD")
print(" - git status"+CONSOLE_SHA,sha,CONSOLE_NORMAL)
store_commit(p.rcommit,sha)
p.rcommit = sha
# Parse the arguments
parser = argparse.ArgumentParser(description='Package Manager. Use pkgman.py -h for help')
parser.add_argument('-v', '--verbose', action='store_true', help='Show extra information')
subparsers = parser.add_subparsers(title='Command to execute', metavar='command', dest='command')
# All the subcommands and arguments
subparsers.add_parser('clean', help='Remove temp folders')
subparsers.add_parser('list', help='List Packages')
subparsers.add_parser('status', help='Show the Status of all the Packages')
subparser_update = subparsers.add_parser('update', help='Download Remote Package Updates')
subparser_update.add_argument('-i', '--id', metavar='id', default='0', type=int, help='Package Number')
# Process arguments or show help if none are given
if len(sys.argv[1:])==0:
parser.print_help()
parser.exit()
args = parser.parse_args()
if args.verbose:
print("paparazzi_pkgman",args)
# Call commands
if args.command == 'list':
pkgman_list(args)
elif args.command == 'clean':
pkgman_clean(args)
elif args.command == 'update':
pkgman_update(args)
elif args.command == 'status':
pkgman_status(args)