Rename file until get file from a host with Fabric











up vote
0
down vote

favorite












I am getting files from a host that will be consumed for another system. I realized I need to rename the files while it is being downloading from the remote server to avoid data corruption. Do you know if there is a way to achieve it, maybe the get() function in combination with StringIO ? or is not possible with fabric?



Edited: Please have a sample of my code.



# ~/fabfile.py
from fabric.api import task, env, run, settings, cd, put, get, execute

@task
def send_files():
'''
Send the downloaded files (found.txt) from remote to the server
'''
# Get the sorted list of filenames (to send the files in order)
with settings(war_only=True):
with cd(REMOTE_DIR):
sorted_list = sort_files()
for file in sorted_list:
print(file)
file = file.replace('n', '')
#Something here to change the extension
#when the download is complete change to the original extension
# Example
# get 427783.zip
# change to 427783.crdownload
# back to 427783.zip when is done.
get(REMOTE_DIR + DESTINATION + '/' + file, INPUT_FOLDER + '\' + file)


I think I have to re-do my question: I would like to know when the download is completed using fabric ftp connection and the get() method, another service will pick up the file and would like to avoid the download is not completed yet.










share|improve this question
























  • Can you be more specific and maybe post your code?
    – Peshmerge
    Nov 12 at 12:34










  • @Peshmerge Yes, please check the code I just add, hope it can give you a better idea. Thank you so much.
    – Sora
    Nov 13 at 1:52















up vote
0
down vote

favorite












I am getting files from a host that will be consumed for another system. I realized I need to rename the files while it is being downloading from the remote server to avoid data corruption. Do you know if there is a way to achieve it, maybe the get() function in combination with StringIO ? or is not possible with fabric?



Edited: Please have a sample of my code.



# ~/fabfile.py
from fabric.api import task, env, run, settings, cd, put, get, execute

@task
def send_files():
'''
Send the downloaded files (found.txt) from remote to the server
'''
# Get the sorted list of filenames (to send the files in order)
with settings(war_only=True):
with cd(REMOTE_DIR):
sorted_list = sort_files()
for file in sorted_list:
print(file)
file = file.replace('n', '')
#Something here to change the extension
#when the download is complete change to the original extension
# Example
# get 427783.zip
# change to 427783.crdownload
# back to 427783.zip when is done.
get(REMOTE_DIR + DESTINATION + '/' + file, INPUT_FOLDER + '\' + file)


I think I have to re-do my question: I would like to know when the download is completed using fabric ftp connection and the get() method, another service will pick up the file and would like to avoid the download is not completed yet.










share|improve this question
























  • Can you be more specific and maybe post your code?
    – Peshmerge
    Nov 12 at 12:34










  • @Peshmerge Yes, please check the code I just add, hope it can give you a better idea. Thank you so much.
    – Sora
    Nov 13 at 1:52













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am getting files from a host that will be consumed for another system. I realized I need to rename the files while it is being downloading from the remote server to avoid data corruption. Do you know if there is a way to achieve it, maybe the get() function in combination with StringIO ? or is not possible with fabric?



Edited: Please have a sample of my code.



# ~/fabfile.py
from fabric.api import task, env, run, settings, cd, put, get, execute

@task
def send_files():
'''
Send the downloaded files (found.txt) from remote to the server
'''
# Get the sorted list of filenames (to send the files in order)
with settings(war_only=True):
with cd(REMOTE_DIR):
sorted_list = sort_files()
for file in sorted_list:
print(file)
file = file.replace('n', '')
#Something here to change the extension
#when the download is complete change to the original extension
# Example
# get 427783.zip
# change to 427783.crdownload
# back to 427783.zip when is done.
get(REMOTE_DIR + DESTINATION + '/' + file, INPUT_FOLDER + '\' + file)


I think I have to re-do my question: I would like to know when the download is completed using fabric ftp connection and the get() method, another service will pick up the file and would like to avoid the download is not completed yet.










share|improve this question















I am getting files from a host that will be consumed for another system. I realized I need to rename the files while it is being downloading from the remote server to avoid data corruption. Do you know if there is a way to achieve it, maybe the get() function in combination with StringIO ? or is not possible with fabric?



Edited: Please have a sample of my code.



# ~/fabfile.py
from fabric.api import task, env, run, settings, cd, put, get, execute

@task
def send_files():
'''
Send the downloaded files (found.txt) from remote to the server
'''
# Get the sorted list of filenames (to send the files in order)
with settings(war_only=True):
with cd(REMOTE_DIR):
sorted_list = sort_files()
for file in sorted_list:
print(file)
file = file.replace('n', '')
#Something here to change the extension
#when the download is complete change to the original extension
# Example
# get 427783.zip
# change to 427783.crdownload
# back to 427783.zip when is done.
get(REMOTE_DIR + DESTINATION + '/' + file, INPUT_FOLDER + '\' + file)


I think I have to re-do my question: I would like to know when the download is completed using fabric ftp connection and the get() method, another service will pick up the file and would like to avoid the download is not completed yet.







python file-io fabric






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 1:54

























asked Nov 12 at 3:03









Sora

458




458












  • Can you be more specific and maybe post your code?
    – Peshmerge
    Nov 12 at 12:34










  • @Peshmerge Yes, please check the code I just add, hope it can give you a better idea. Thank you so much.
    – Sora
    Nov 13 at 1:52


















  • Can you be more specific and maybe post your code?
    – Peshmerge
    Nov 12 at 12:34










  • @Peshmerge Yes, please check the code I just add, hope it can give you a better idea. Thank you so much.
    – Sora
    Nov 13 at 1:52
















Can you be more specific and maybe post your code?
– Peshmerge
Nov 12 at 12:34




Can you be more specific and maybe post your code?
– Peshmerge
Nov 12 at 12:34












@Peshmerge Yes, please check the code I just add, hope it can give you a better idea. Thank you so much.
– Sora
Nov 13 at 1:52




@Peshmerge Yes, please check the code I just add, hope it can give you a better idea. Thank you so much.
– Sora
Nov 13 at 1:52












2 Answers
2






active

oldest

votes

















up vote
0
down vote













Using wget will solve your problem. You can do the following:



 with connection(host=host, user=user) as c:
save_path = '/var/www/new_name.extension'
source_path = 'where_your_file_resides'
c.run('wget -p -O %s %s' %(save_path, source_path)





share|improve this answer























  • Please correct me if I am wrong, in my code file_path_you_want_to_download will be the path where I want to store the file when downloaded, in my code INPUT_FOLDER, right?
    – Sora
    Nov 13 at 13:55










  • Please check my edit!
    – Peshmerge
    Nov 13 at 14:19










  • I try your code, however I think it does not work because the file resides in the same server I connect to. wget is not for external url?
    – Sora
    Nov 20 at 7:02


















up vote
0
down vote













I did not find any suitable example to change the extension automatically when the downloading is still in process. So, I figured it out and download the file with another a temporary extension, once the get() function finish fabric will execute the next command, so I take advantage of this and rename the file.



Like this:



def rename_file(original, output):
try:
os.rename(original, output)
except WindowsError:
os.remove(output)
os.rename(original, output)

@task
def send_files():
with settings(war_only=True):
with cd(REMOTE_DIR):
sorted_list = sort_files()
for file in sorted_list:
old_file = REMOTE_DIR + DESTINATION + '/' + file
temp_file = INPUT_OTDF_FOLDER + '\' + file + '.tmp'

get(old_file, temp_file)
rename_file(temp_file, new_file)





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255457%2frename-file-until-get-file-from-a-host-with-fabric%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Using wget will solve your problem. You can do the following:



     with connection(host=host, user=user) as c:
    save_path = '/var/www/new_name.extension'
    source_path = 'where_your_file_resides'
    c.run('wget -p -O %s %s' %(save_path, source_path)





    share|improve this answer























    • Please correct me if I am wrong, in my code file_path_you_want_to_download will be the path where I want to store the file when downloaded, in my code INPUT_FOLDER, right?
      – Sora
      Nov 13 at 13:55










    • Please check my edit!
      – Peshmerge
      Nov 13 at 14:19










    • I try your code, however I think it does not work because the file resides in the same server I connect to. wget is not for external url?
      – Sora
      Nov 20 at 7:02















    up vote
    0
    down vote













    Using wget will solve your problem. You can do the following:



     with connection(host=host, user=user) as c:
    save_path = '/var/www/new_name.extension'
    source_path = 'where_your_file_resides'
    c.run('wget -p -O %s %s' %(save_path, source_path)





    share|improve this answer























    • Please correct me if I am wrong, in my code file_path_you_want_to_download will be the path where I want to store the file when downloaded, in my code INPUT_FOLDER, right?
      – Sora
      Nov 13 at 13:55










    • Please check my edit!
      – Peshmerge
      Nov 13 at 14:19










    • I try your code, however I think it does not work because the file resides in the same server I connect to. wget is not for external url?
      – Sora
      Nov 20 at 7:02













    up vote
    0
    down vote










    up vote
    0
    down vote









    Using wget will solve your problem. You can do the following:



     with connection(host=host, user=user) as c:
    save_path = '/var/www/new_name.extension'
    source_path = 'where_your_file_resides'
    c.run('wget -p -O %s %s' %(save_path, source_path)





    share|improve this answer














    Using wget will solve your problem. You can do the following:



     with connection(host=host, user=user) as c:
    save_path = '/var/www/new_name.extension'
    source_path = 'where_your_file_resides'
    c.run('wget -p -O %s %s' %(save_path, source_path)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 at 14:17

























    answered Nov 13 at 13:30









    Peshmerge

    264317




    264317












    • Please correct me if I am wrong, in my code file_path_you_want_to_download will be the path where I want to store the file when downloaded, in my code INPUT_FOLDER, right?
      – Sora
      Nov 13 at 13:55










    • Please check my edit!
      – Peshmerge
      Nov 13 at 14:19










    • I try your code, however I think it does not work because the file resides in the same server I connect to. wget is not for external url?
      – Sora
      Nov 20 at 7:02


















    • Please correct me if I am wrong, in my code file_path_you_want_to_download will be the path where I want to store the file when downloaded, in my code INPUT_FOLDER, right?
      – Sora
      Nov 13 at 13:55










    • Please check my edit!
      – Peshmerge
      Nov 13 at 14:19










    • I try your code, however I think it does not work because the file resides in the same server I connect to. wget is not for external url?
      – Sora
      Nov 20 at 7:02
















    Please correct me if I am wrong, in my code file_path_you_want_to_download will be the path where I want to store the file when downloaded, in my code INPUT_FOLDER, right?
    – Sora
    Nov 13 at 13:55




    Please correct me if I am wrong, in my code file_path_you_want_to_download will be the path where I want to store the file when downloaded, in my code INPUT_FOLDER, right?
    – Sora
    Nov 13 at 13:55












    Please check my edit!
    – Peshmerge
    Nov 13 at 14:19




    Please check my edit!
    – Peshmerge
    Nov 13 at 14:19












    I try your code, however I think it does not work because the file resides in the same server I connect to. wget is not for external url?
    – Sora
    Nov 20 at 7:02




    I try your code, however I think it does not work because the file resides in the same server I connect to. wget is not for external url?
    – Sora
    Nov 20 at 7:02












    up vote
    0
    down vote













    I did not find any suitable example to change the extension automatically when the downloading is still in process. So, I figured it out and download the file with another a temporary extension, once the get() function finish fabric will execute the next command, so I take advantage of this and rename the file.



    Like this:



    def rename_file(original, output):
    try:
    os.rename(original, output)
    except WindowsError:
    os.remove(output)
    os.rename(original, output)

    @task
    def send_files():
    with settings(war_only=True):
    with cd(REMOTE_DIR):
    sorted_list = sort_files()
    for file in sorted_list:
    old_file = REMOTE_DIR + DESTINATION + '/' + file
    temp_file = INPUT_OTDF_FOLDER + '\' + file + '.tmp'

    get(old_file, temp_file)
    rename_file(temp_file, new_file)





    share|improve this answer

























      up vote
      0
      down vote













      I did not find any suitable example to change the extension automatically when the downloading is still in process. So, I figured it out and download the file with another a temporary extension, once the get() function finish fabric will execute the next command, so I take advantage of this and rename the file.



      Like this:



      def rename_file(original, output):
      try:
      os.rename(original, output)
      except WindowsError:
      os.remove(output)
      os.rename(original, output)

      @task
      def send_files():
      with settings(war_only=True):
      with cd(REMOTE_DIR):
      sorted_list = sort_files()
      for file in sorted_list:
      old_file = REMOTE_DIR + DESTINATION + '/' + file
      temp_file = INPUT_OTDF_FOLDER + '\' + file + '.tmp'

      get(old_file, temp_file)
      rename_file(temp_file, new_file)





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        I did not find any suitable example to change the extension automatically when the downloading is still in process. So, I figured it out and download the file with another a temporary extension, once the get() function finish fabric will execute the next command, so I take advantage of this and rename the file.



        Like this:



        def rename_file(original, output):
        try:
        os.rename(original, output)
        except WindowsError:
        os.remove(output)
        os.rename(original, output)

        @task
        def send_files():
        with settings(war_only=True):
        with cd(REMOTE_DIR):
        sorted_list = sort_files()
        for file in sorted_list:
        old_file = REMOTE_DIR + DESTINATION + '/' + file
        temp_file = INPUT_OTDF_FOLDER + '\' + file + '.tmp'

        get(old_file, temp_file)
        rename_file(temp_file, new_file)





        share|improve this answer












        I did not find any suitable example to change the extension automatically when the downloading is still in process. So, I figured it out and download the file with another a temporary extension, once the get() function finish fabric will execute the next command, so I take advantage of this and rename the file.



        Like this:



        def rename_file(original, output):
        try:
        os.rename(original, output)
        except WindowsError:
        os.remove(output)
        os.rename(original, output)

        @task
        def send_files():
        with settings(war_only=True):
        with cd(REMOTE_DIR):
        sorted_list = sort_files()
        for file in sorted_list:
        old_file = REMOTE_DIR + DESTINATION + '/' + file
        temp_file = INPUT_OTDF_FOLDER + '\' + file + '.tmp'

        get(old_file, temp_file)
        rename_file(temp_file, new_file)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 3:48









        Sora

        458




        458






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255457%2frename-file-until-get-file-from-a-host-with-fabric%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Berounka

            Sphinx de Gizeh

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...