-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchive
executable file
·154 lines (130 loc) · 4.54 KB
/
archive
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
#!/usr/bin/python3
import git
import os
import pickle
import sys
import time
import zipfile
# script variables
config_file_name = 'store.pckl'
# handle the config file
class pickleh():
def get(self):
if not os.path.isfile(config_file_name):
self.create()
f = open(config_file_name, 'rb')
config = pickle.load(f)
f.close()
return config
def store(self, attr, value):
config = self.get(self)
config[attr] = value
f = open(config_file_name, 'wb')
pickle.dump(config, f)
f.close()
return config
def attr(self, attr):
config = self.get(self)
return config.get(attr, '')
def destroy():
os.remove(config_file_name)
def create():
f = open(config_file_name, 'wb')
pickle.dump({}, f)
f.close()
# check config keys existence
class initConfig():
def __init__(self):
args = self.args()
if args['--flush'] == True:
pickleh.destroy()
pickleh.create()
# check if branch_name is undefined
self.checkBranchName()
# check config keys exist
self.latestCommit()
def ret(self):
return pickleh.get(pickleh)
def checkBranchName(self):
if len(pickleh.attr(pickleh, 'branch_name')) < 1:
pickleh.store(pickleh, 'branch_name', 'master')
def latestCommit(self):
if len(pickleh.attr(pickleh, 'last_commit_zipped')) != 40:
latest = input("Please enter a commit hash to compare from: ")
if len(latest) != 40:
print('Error: Hash should be 40 charchters length 👊')
self.latestCommit()
else:
pickleh.store(pickleh, 'last_commit_zipped', latest)
def args(self):
arguments = {
'--branch' : 'master',
'--flush' : False,
}
for arg in sys.argv[1:]:
name = arg.split('=')
arguments[name[0]] = name[1] if len(name) > 1 else True
if (name[0] == '--branch'):
pickleh.store(pickleh, 'branch_name', arguments['--branch'])
return arguments
# main function
class gitAmrography():
def __init__(self):
global repo
global g
global git_path
global config
global branch_name
c = initConfig()
config = c.ret()
branch_name = config['branch_name']
git_path = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
repo = git.Repo(git_path)
g = git.Git(git_path)
def gitLatestCommit(self):
return str(repo.heads[branch_name].commit.tree)
def gitDiff(self, branch1, branch2):
format = '--name-only'
commits = []
differ = g.diff('%s..%s' % (branch1, branch2), format).split("\n")
for line in differ:
if len(line):
commits.append(line)
return commits
def package(self):
latestcommit = self.gitLatestCommit()
commits = self.gitDiff(latestcommit, config['last_commit_zipped'])
commits_length = len(commits)
if commits_length > 0:
self.packagePrepare(commits, latestcommit)
print('\n🥳 Package is ready')
print('⛓ Commits affected = ' + str(commits_length) )
else:
print('\n✅ No changes to package')
print('🍴 Branch -> ' + branch_name)
def packagePrepare(self, commits, latestcommit):
new_archive_dir = "archives/" + branch_name + "_" + str(int(time.time()))
if not os.path.exists(new_archive_dir):
os.makedirs(new_archive_dir)
zipf = zipfile.ZipFile(new_archive_dir + '/archive.zip', 'w', zipfile.ZIP_DEFLATED)
self.packageAdd(commits, zipf, new_archive_dir)
zipf.close()
pickleh.store(pickleh, 'last_commit_zipped', latestcommit)
def packageAdd(self, commits, ziph, archive_directory):
deleted_file = archive_directory + "/deleted_files.txt"
changed_file = archive_directory + "/changed_files.txt"
fd = open(deleted_file ,"w+")
fc = open(changed_file ,"w+")
for commit in commits:
file = "../" + commit
if os.path.isfile(file):
print('✅ File added ', commit)
fc.write("%s\n" % (commit))
ziph.write(file)
else:
print('❌ File deleted ', commit)
fd.write("%s\n" % (commit))
fd.close()
fc.close()
archive = gitAmrography()
archive.package()