2022-04-20 05:36:14 +02:00
"""
A small install script to download the correct hishtory binary for the current OS / architecture .
The hishtory binary is in charge of installing itself , this just downloads the correct binary and
executes it .
"""
import json
import urllib . request
import platform
import sys
import os
2024-02-10 05:21:27 +01:00
def get_executable_tmpdir ( ) :
specified_dir = os . environ . get ( ' TMPDIR ' , ' ' )
if specified_dir :
return specified_dir
try :
if hasattr ( os , ' ST_NOEXEC ' ) :
if os . statvfs ( " /tmp " ) . f_flag & os . ST_NOEXEC :
# /tmp/ is mounted as NOEXEC, so fall back to the current working directory
return os . getcwd ( )
except :
pass
return " /tmp/ "
2022-04-20 05:36:14 +02:00
with urllib . request . urlopen ( ' https://api.hishtory.dev/api/v1/download ' ) as response :
resp_body = response . read ( )
download_options = json . loads ( resp_body )
2022-12-12 05:39:45 +01:00
if platform . system ( ) == ' Linux ' and platform . machine ( ) == " x86_64 " :
2022-04-20 05:36:14 +02:00
download_url = download_options [ ' linux_amd_64_url ' ]
2022-12-13 04:45:54 +01:00
elif platform . system ( ) == ' Linux ' and platform . machine ( ) == " aarch64 " :
2022-12-12 05:39:45 +01:00
download_url = download_options [ ' linux_arm_64_url ' ]
2023-02-19 07:00:39 +01:00
elif platform . system ( ) == ' Linux ' and platform . machine ( ) == " armv7l " :
2023-09-07 06:35:28 +02:00
download_url = download_options [ ' linux_arm_7_url ' ]
2022-04-20 05:36:14 +02:00
elif platform . system ( ) == ' Darwin ' and platform . machine ( ) == ' arm64 ' :
download_url = download_options [ ' darwin_arm_64_url ' ]
elif platform . system ( ) == ' Darwin ' and platform . machine ( ) == ' x86_64 ' :
download_url = download_options [ ' darwin_amd_64_url ' ]
else :
print ( f " No hishtory binary for system= { platform . system ( ) } , machine= { platform . machine ( ) } ! \n If you believe this is a mistake, please open an issue here: https://github.com/ddworken/hishtory/issues " )
sys . exit ( 1 )
with urllib . request . urlopen ( download_url ) as response :
hishtory_binary = response . read ( )
2022-11-04 02:04:16 +01:00
2024-02-10 02:41:52 +01:00
tmpFilePath = os . path . join ( get_executable_tmpdir ( ) , ' hishtory-client ' )
2022-11-04 02:04:16 +01:00
if os . path . exists ( tmpFilePath ) :
os . remove ( tmpFilePath )
with open ( tmpFilePath , ' wb ' ) as f :
2022-04-20 05:36:14 +02:00
f . write ( hishtory_binary )
2022-11-04 02:04:16 +01:00
os . system ( ' chmod +x ' + tmpFilePath )
2023-12-22 04:03:21 +01:00
cmd = tmpFilePath + ' install '
if os . environ . get ( ' HISHTORY_OFFLINE ' ) :
cmd + = " --offline "
exitCode = os . system ( cmd )
2024-02-10 05:20:55 +01:00
os . remove ( tmpFilePath )
2022-06-05 07:09:30 +02:00
if exitCode != 0 :
2022-11-04 02:04:16 +01:00
raise Exception ( " failed to install downloaded hishtory client via ` " + tmpFilePath + " install` (is that directory mounted noexec? Consider setting an alternate directory via the TMPDIR environment variable)! " )
2022-04-20 07:22:04 +02:00
print ( ' Succesfully installed hishtory! Open a new terminal, try running a command, and then running `hishtory query`. ' )