2019-11-25 08:42:18 -06:00
#! /usr/bin/env python
2020-02-24 08:57:56 -06:00
""" Upload socializer to the ftp server. This script has been created to be executed from a continuous integrations system.
Important note : for this script to work , the following conditions should be met :
2020-06-29 09:51:55 -05:00
* There must be ftp server data , via environment variables ( FTP_SERVER , FTP_USERNAME and FTP_PASSWORD ) or via arguments to the script call ( in the prior order ) . Connection to this server is done via default ftp port via TLS .
* this code assumes it ' s going to connect to an FTP via TLS.
2020-02-24 08:57:56 -06:00
* If the version to upload is alpha , there ' s not need of an extra variable. Otherwise, CI_COMMIT_TAG should point to a version as vx.x, where v is a literal and x are numbers, example may be v0.18, v0.25, v0.3. This variable should be set in the environment.
* Inside the ftp server , the following directory structure will be expected : socializer . su / static / files / . The script will create the < version > folder or alpha if needed .
* The script will upload all . exe , . zip and . json files located in the root directory from where it was called . The json files are uploaded to socializer . su / static / files / update and other files are going to socializer . su / static / files / < version > .
"""
2019-11-25 08:42:18 -06:00
import sys
2019-11-25 10:49:58 -06:00
import os
import glob
2019-11-25 08:42:18 -06:00
import ftplib
2019-11-25 10:49:58 -06:00
transferred = 0
2020-06-29 10:27:28 -05:00
class MyFTP_TLS ( ftplib . FTP_TLS ) :
""" Explicit FTPS, with shared TLS session """
def ntransfercmd ( self , cmd , rest = None ) :
conn , size = ftplib . FTP . ntransfercmd ( self , cmd , rest )
if self . _prot_p :
conn = self . context . wrap_socket ( conn ,
server_hostname = self . host ,
session = self . sock . session ) # this is the fix
return conn , size
2019-11-25 10:49:58 -06:00
def convert_bytes ( n ) :
2020-02-24 08:57:56 -06:00
K , M , G , T , P = 1 << 10 , 1 << 20 , 1 << 30 , 1 << 40 , 1 << 50
if n > = P :
return ' %.2f Pb ' % ( float ( n ) / T )
elif n > = T :
return ' %.2f Tb ' % ( float ( n ) / T )
elif n > = G :
return ' %.2f Gb ' % ( float ( n ) / G )
elif n > = M :
return ' %.2f Mb ' % ( float ( n ) / M )
elif n > = K :
return ' %.2f Kb ' % ( float ( n ) / K )
else :
return ' %d ' % n
2019-11-25 10:49:58 -06:00
def callback ( progress ) :
global transferred
transferred = transferred + len ( progress )
print ( " Uploaded {} " . format ( convert_bytes ( transferred ) , ) )
ftp_server = os . environ . get ( " FTP_SERVER " ) or sys . argv [ 1 ]
ftp_username = os . environ . get ( " FTP_USERNAME " ) or sys . argv [ 2 ]
ftp_password = os . environ . get ( " FTP_PASSWORD " ) or sys . argv [ 3 ]
2019-12-10 10:43:55 -06:00
version = os . environ . get ( " CI_COMMIT_TAG " ) or " alpha "
2019-11-25 10:49:58 -06:00
version = version . replace ( " v " , " " )
print ( " Uploading files to the Socializer server... " )
2021-01-05 16:05:56 -06:00
print ( " Connecting to %s " % ( ftp_server , ) )
2021-01-18 13:36:19 -06:00
connection = ftplib . FTP ( ftp_server )
2019-11-25 10:49:58 -06:00
print ( " Connected to FTP server {} " . format ( ftp_server , ) )
connection . login ( user = ftp_username , passwd = ftp_password )
2021-01-18 13:36:19 -06:00
#connection.prot_p()
2019-11-25 10:49:58 -06:00
print ( " Logged in successfully " )
2021-01-18 13:36:19 -06:00
connection . cwd ( " socializer " )
2019-11-25 10:49:58 -06:00
if version not in connection . nlst ( ) :
print ( " Creating version directory {} because does not exists... " . format ( version , ) )
connection . mkd ( version )
2019-12-11 11:13:52 -06:00
if " update " not in connection . nlst ( ) :
print ( " Creating update info directory because does not exists... " )
connection . mkd ( " update " )
2019-11-25 10:49:58 -06:00
connection . cwd ( version )
print ( " Moved into version directory " )
2019-12-11 11:13:52 -06:00
files = glob . glob ( " *.zip " ) + glob . glob ( " *.exe " ) + glob . glob ( " *.json " )
2019-11-25 10:49:58 -06:00
print ( " These files will be uploaded into the version folder: {} " . format ( files , ) )
for file in files :
transferred = 0
print ( " Uploading {} " . format ( file , ) )
with open ( file , " rb " ) as f :
2019-12-11 11:13:52 -06:00
if file . endswith ( " json " ) :
connection . storbinary ( ' STOR ../update/ %s ' % file , f , callback = callback , blocksize = 1024 * 1024 )
else :
connection . storbinary ( ' STOR %s ' % file , f , callback = callback , blocksize = 1024 * 1024 )
2019-11-25 10:49:58 -06:00
print ( " Upload completed. exiting... " )
connection . quit ( )