본문 바로가기
Python

펌웨어 자동 업데이트 시스템 구축 with python

by 행복한 기린님 2022. 8. 3.

Jetson nano를 활용해 솔루션을 만들고 난 뒤 

 

코드가 업데이트 됐을때 펌웨어를 자동으로 업데이트 할 수 있도록 업데이트 시스템을 구축했다.

 

 

시나리오는 아래와같다.

 

1. 서버에 버전을 관리하는 파일을 활용해 버전을 리스트로 반환하는 API를 만든다.

2. 서버에 펌웨어를 구성하는 파일을 압축하여 버전명으로 된 폴더에 보관한다.

3. 클라이언트에서 버전 리스트 API를 활용해 현재 버전과 최신 버전을 비교한다.

4. 최신 버전과 현재 버전이 다르다면 자동 업데이트 코드를 수행한다.

 

 

자동 업데이트 코드

1. 실행시 버전 명을 변수로 사용해 버전에 맞는 압축 파일을 다운받는다.

 

# Downloading the file by sending the request to the URL
req = requests.get(url)
print('Downloading Completed')

 

 

2. 다운 받은 파일 압축을 해제한다

 

# extracting the zip file contents
zipfile= zipfile.ZipFile(BytesIO(req.content))
zipfile.extractall('C:/Users/Blades/Downloads/NewFolder')

 

 

3. 압축 해제한 모든 파일을 기존 코드로 옮긴다(덮어쓴다)

 

# get files from extrated folder
extract_files = os.listdir(extract_path)
        
# replace files from extracted file to original folder
for i in extract_files :
    if not i == '__MACOSX':
        os.replace(os.path.join(extract_path, i), os.path.join(os.getcwd(), i))

 

전체 코드

 

import os
import requests
from io import BytesIO
import zipfile

def download_extract_zip(url):
    try :
        # Downloading the file by sending the request to the URL
        req = requests.get(url)
        print('Downloading Completed')

        makedir('saved')

        # extracting the zip file contents
        zip = zipfile.ZipFile(BytesIO(req.content))
        zip.extractall(extract_path)

        # get files from extrated folder
        extract_files = os.listdir(extract_path)

        # replace files from extracted file to original folder
        for i in extract_files :
            if not i == '__MACOSX':
                os.replace(os.path.join(extract_path, i), os.path.join(os.getcwd(), i))

    except Exception as e:
        print(e)


def makedir(directory):
    try :
    	# if folder doesn't exist make folder
        if not os.path.exists(directory):
            os.makedirs(directory)
    except OSError:
        print("Error: Failed to create directory")



if __name__ == "__main__":
    update_url = 'http://.../zipfile.zip'
    download_extract_zip(update_url)

댓글