Skip to content

Instantly share code, notes, and snippets.

@bmwant
Created September 29, 2015 12:53
Show Gist options
  • Save bmwant/e3ee12d8caf5096e18d6 to your computer and use it in GitHub Desktop.
Save bmwant/e3ee12d8caf5096e18d6 to your computer and use it in GitHub Desktop.
Fabric copyfiles
# -*- coding: utf-8 -*-
import os
from fabric.api import env, run, put, cd, local
env.user = ''
env.password = ''
env.hosts = ['127.0.0.1']
env.shell = "bash -c"
path = '/path/to/projects/'
app_path = '{path}app_name/'.format(path=path)
venv_path = '{app_path}venv'.format(app_path=app_path)
DIRS = ('app', 'prod', )
FILES = ('runserver.py', )
BAD_DIRS = ('__pycache__', )
BAD_EXT = ()
def retrieve_files():
"""
Build a list of files that should be deployed to remote server
:return:
"""
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
ALL_FILES = []
ALL_FOLDERS = []
for allowed_dir in DIRS:
print('Walking through', allowed_dir)
for root, dirs, files in os.walk(os.path.join(CURRENT_DIR, allowed_dir)):
skip_dir = False
for bad_dir in BAD_DIRS:
if bad_dir in root:
skip_dir = True
break
if skip_dir:
continue
ALL_FOLDERS.append(remove_line_beginning(root, CURRENT_DIR))
for file_name in files:
full_path_to_file = os.path.join(root, file_name)
file_ext = os.path.splitext(file_name)[1]
if file_ext in BAD_EXT:
continue
actual_file_path = remove_line_beginning(full_path_to_file, CURRENT_DIR)
ALL_FILES.append((full_path_to_file, actual_file_path))
#print(full_path_to_file)
print('All files to include:')
print('\n'.join(map(lambda x: x[0], ALL_FILES)))
print('All folders to create')
print('\n'.join(ALL_FOLDERS))
return ALL_FILES, ALL_FOLDERS
def remove_line_beginning(line, beginning):
assert isinstance(line, str)
assert isinstance(beginning, str)
if not line.startswith(beginning):
raise ValueError('%s doesn\'t begin with %s' % (line, beginning))
without_beginning = line[len(beginning):]
return change_slashes(without_beginning)
def change_slashes(line):
return line.replace('\\', '/')
def deploy():
"""
Delpoy project to remote server
"""
files, folders = retrieve_files()
for folder in folders:
remote_path = 'thedir' + folder
run('mkdir -p %s' % remote_path)
for file_info in files:
remote_path = 'thedir' + file_info[1]
put(file_info[0], remote_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment