Skip to content

Commit

Permalink
Python codestyle changes
Browse files Browse the repository at this point in the history
  • Loading branch information
wiro committed Dec 25, 2018
1 parent 1ae4e7f commit 622518e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
39 changes: 26 additions & 13 deletions gitlab-project-export.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@
epilog='Created by Robert Vojcik <[email protected]>')

# Arguments
parser.add_argument('-c', dest='config', default='config.yaml',
help='config file')
parser.add_argument('-d', dest='debug', default=False, action='store_const', const=True,
help='Debug mode')
parser.add_argument(
'-c', dest='config', default='config.yaml',
help='config file'
)
parser.add_argument(
'-d', dest='debug', default=False, action='store_const',
const=True, help='Debug mode'
)

args = parser.parse_args()

Expand All @@ -42,39 +46,44 @@

# Init gitlab api object
if args.debug:
print("%s, token"%(gitlab_url))
print("%s, token" % (gitlab_url))
gitlab = gitlab.Api(gitlab_url, token)

# Export each project
for project in c.config["gitlab"]["projects"]:
if args.debug:
print("Exporting %s"%(project))
print("Exporting %s" % (project))
status = gitlab.project_export(project)

# Export successful
if status:
if args.debug:
print("Success for %s"%(project))
print("Success for %s" % (project))
# Download project to our destination
if c.config["backup"]["project_dirs"]:
destination = c.config["backup"]["destination"] + "/" + project
else:
destination = c.config["backup"]["destination"]

if args.debug:
print(" Destination %s"%(destination))
print(" Destination %s" % (destination))

# Prepare actual date
d = date.today()
# File template from config
file_tmpl = c.config["backup"]["backup_name"]
# Projectname in dest_file
dest_file = destination + "/" + file_tmpl.replace("{PROJECT_NAME}", project.replace("/", "-"))
dest_file = destination + "/" + file_tmpl.replace(
"{PROJECT_NAME}",
project.replace("/", "-")
)
# Date in dest_file
dest_file = dest_file.replace("{TIME}",d.strftime(c.config["backup"]["backup_time_format"]))
dest_file = dest_file.replace(
"{TIME}", d.strftime(c.config["backup"]["backup_time_format"])
)

if args.debug:
print(" Destination file %s"%(dest_file))
print(" Destination file %s" % (dest_file))

# Create directories
if not os.path.isdir(destination):
Expand All @@ -83,10 +92,14 @@
# Get URL from gitlab object
url = gitlab.download_url["api_url"]
if args.debug:
print(" URL: %s"%(url))
print(" URL: %s" % (url))

# Download file
r = requests.get(url, allow_redirects=True, stream=True, headers={"PRIVATE-TOKEN": token})
r = requests.get(
url,
allow_redirects=True,
stream=True,
headers={"PRIVATE-TOKEN": token})

with open(dest_file, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
Expand Down
27 changes: 15 additions & 12 deletions gitlab-project-import.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@
epilog='Created by Robert Vojcik <[email protected]>')

# Arguments
parser.add_argument('-c', dest='config', default='config.yaml',
help='config file')
parser.add_argument('-f', dest='filepath', default=False,
help='Path to gitlab exported project file')
parser.add_argument('-p', dest='project_path', default=False,
help='Project path')
parser.add_argument('-d', dest='debug', default=False, action='store_const', const=True,
help='Debug mode')
parser.add_argument(
'-c', dest='config', default='config.yaml',
help='config file')
parser.add_argument(
'-f', dest='filepath', default=False,
help='Path to gitlab exported project file')
parser.add_argument(
'-p', dest='project_path', default=False,
help='Project path')
parser.add_argument(
'-d', dest='debug', default=False, action='store_const', const=True,
help='Debug mode')

args = parser.parse_args()

Expand All @@ -46,18 +50,18 @@

# Init gitlab api object
if args.debug:
print("%s, token"%(gitlab_url))
print("%s, token" % (gitlab_url))
gitlab = gitlab.Api(gitlab_url, token)

# import project
if args.project_path and args.filepath and os.path.isfile(args.filepath):
if args.debug:
print("Exporting %s"%(args.project_path))
print("Exporting %s" % (args.project_path))
status = gitlab.project_import(args.project_path, args.filepath)

# Import successful
if status:
print("Import success for %s"%(args.project_path))
print("Import success for %s" % (args.project_path))
sys.exit(0)
else:
print("Import was not successful")
Expand All @@ -66,4 +70,3 @@
else:
print("Error, you have to specify correct project_path and filepath")
sys.exit(1)

0 comments on commit 622518e

Please sign in to comment.